package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xrit.sunnyday.dao.MemberDao;
import com.xrit.sunnyday.dao.OrderDao;
import com.xrit.sunnyday.dao.OrderSettingDao;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.Member;
import com.xrit.sunnyday.pojo.Order;
import com.xrit.sunnyday.pojo.OrderSetting;
import com.xrit.sunnyday.service.OrderService;
import com.xrit.sunnyday.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;
    @Override
    @Transactional
    public Order submitOrder(Map<String, String> paraMap) throws SunnydayException {
        /* 利用数据库现成的数据，测一测
        System.out.println("预约成功！");
        Order order = new Order();
        order.setId(17);
        return order;*/

        //1.获取前端页面填写的日期
        String orderDateStr = paraMap.get("orderDate");
        Date orderDate = null;
        try {
            //1.1转换成日期字符串
            orderDate = DateUtils.parseString2Date(orderDateStr);
        } catch (Exception e) {
            throw new SunnydayException("日期格式有误！");
        }
        //2,通过日期查询当天的预约信息
        OrderSetting orderSetting = orderSettingDao.findByOrderData(orderDate);
        //3.判断这个预约信息是否存在(存在就代表医生设置了该天可以上班)
        if (null==orderSetting){
            throw new SunnydayException("所选日期，不能预约！");
        }
        //4.存在就判断是否预约满
        if (orderSetting.getReservations()>=orderSetting.getNumber()){
            throw new SunnydayException("所选日期，预约人数已满员！");
        }
        //5.判断是否是会员
        //5.1取手机号
        String telephone = paraMap.get("telephone");
        //5.2拿手机号作为条件去查会员表
        Member member = memberDao.findByTelephone(telephone);
        //6.判断是否是会员
        if (null==member){
            //6.1没有该会员,拿用户手机注册一个
            member = new Member();
            member.setName(paraMap.get("name"));
            member.setIdCard(paraMap.get("idCard"));
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            member.setSex(paraMap.get("sex"));
            //7.添加会员进数据库
            memberDao.add(member);
        }
        //7.是会员,判断是否重复预约，同一个人member_id,同一天orderDate同一个套餐setmeal
        //最终就是用这3个信息构建一个Order作为条件去查询Order表
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setSetmealId(Integer.valueOf(paraMap.get("setmealId")));
        //7.1条件查询
        List<Order> orderList = orderDao.findByCondition(order);
        //8.判断是否重复预约
        if (null!=orderList&&orderList.size()>0){
            //8.1存在，报错
            throw new SunnydayException("不能重复预约！");
        }
        //8.1补充订单信息
        order.setOrderType(paraMap.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //9.添加Order到数据库
        orderDao.add(order);
        //10.修改ordersetting表中的已预约人数
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return order;
    }

    @Override
    public Map<String, Object> findById4Detail(int id) {
        return orderDao.findById4Detail(id);
    }
}
